package cn.itmantzy.pet.controller;

import cn.itmantzy.basic.exception.BusinessException;
import cn.itmantzy.basic.util.AjaxResult;
import cn.itmantzy.basic.util.LoginContext;
import cn.itmantzy.basic.util.PageList;
import cn.itmantzy.pet.domain.Pet;
import cn.itmantzy.pet.domain.SearchMasterMsg;
import cn.itmantzy.pet.query.SearchMasterMsgQuery;
import cn.itmantzy.pet.service.ISearchMasterMsgService;
import cn.itmantzy.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;


    @GetMapping
    public List<SearchMasterMsg> loadAll(){
        return searchMasterMsgService.queryAll();
    }

    /**
     * 寻主消息接口
     * @param masterMsg
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg masterMsg, HttpServletRequest request){
        try {
            searchMasterMsgService.publish(masterMsg, request);
            return AjaxResult.getAjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统繁忙，我们正在殴打程序员!!!");
        }
    }

    /**
     * 查询待处理消息
     * @return
     */
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        // 待处理消息state为0
        query.setState(0);
        Logininfo logininfo = LoginContext.getCurrentLogininfo(request);
        return searchMasterMsgService.pending(query, logininfo);
    }

    /**
     * 查询已处理消息
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        // 已处理消息state为1
        query.setState(1);
        return searchMasterMsgService.processed(query, LoginContext.getCurrentLogininfo(request));
    }

    /**
     * 用户查看信息
     */
    @PostMapping("/user")
    public PageList user(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        return searchMasterMsgService.user(query, LoginContext.getCurrentLogininfo(request));
    }

    /**
     * 处理消息接口
     * @param pet
     * @param request
     * @return
     */
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet, HttpServletRequest request){
        try {
            searchMasterMsgService.handle(pet, LoginContext.getCurrentLogininfo(request));
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统繁忙，正在全力抢救！！！");
        }
    }

}
